﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DropBoxTool.Model;
using Microsoft.Phone.Controls;
using SharpDropBoxClient;
using SharpDropBoxClient.db;

namespace DropBoxTool
{
    public partial class BrowseFiles : ViewModelPageClassWithQuery
    {
        private const string kDictionaryKey = "Dict";
        private IDictionary<string, object> jsonDict = null;

        public BrowseFiles()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (State.ContainsKey(kDictionaryKey))
            {
                jsonDict = (IDictionary<string, object>)(JsonSerializer.DeserializeObject((string)State[kDictionaryKey]));
            }
            if (jsonDict == null)
            {
                loadDirectory();
            }
            else
            {
                buildUIFromJSON(jsonDict);
            }
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            State[kDictionaryKey] = JsonSerializer.SerializeObject(jsonDict);
        }

        private void buildUIFromJSON(IDictionary<string, object> dict)
        {
            var dirInfo = DBoxListing.FromDictionary(dict);
            FullPath = dirInfo.FullPath;
            FolderIcon = dirInfo.Icon;
            LastModified = dirInfo.LastModified;
            Size = dirInfo.Size;
            SizeInBytes = dirInfo.SizeInBytes;

            Contents.Clear();
            foreach (var item in dirInfo.Contents)
            {
                Contents.Add(NavigatableDropBoxFolderItem.FromBaseType(item));
            }
        }

        private void loadDirectory()
        {
            Dictionary<string, string> queryVars = new Dictionary<string, string>();
            if (query.Length > 0)
            {
                queryVars = parseQS();
            }
            var dirPath = "";
            if (queryVars.ContainsKey("path"))
            {
                dirPath = queryVars["path"];
            }
            DropBoxController.Inst.Api.GetFileEntry(dirPath, (new[] { "list=true" }).ToList(), (dirResult) =>
            {
                if (dirResult.Error == null)
                {
                    jsonDict = dirResult.Dictionary;
                    Dispatcher.BeginInvoke(() =>
                    {
                        buildUIFromJSON(dirResult.Dictionary);
                    })
;
                }
                else
                {
                    // TODO: write some kind of message to the end user
                }
            });
        }

        #region Bindables

        protected string fullPath = "";

        public string FullPath
        {
            get { return fullPath; }
            set
            {
                fullPath = value;
                RaisePropertyChanged("FullPath");
            }
        }

        protected string icon = "";

        public string FolderIcon
        {
            get { return icon; }
            set
            {
                icon = value;
                RaisePropertyChanged("FolderIcon");
            }
        }

        protected string lastModified = "";

        public string LastModified
        {
            get { return lastModified; }
            set
            {
                lastModified = value;
                RaisePropertyChanged("LastModified");
            }
        }

        protected string size = "";

        public string Size
        {
            get { return size; }
            set
            {
                size = value;
                RaisePropertyChanged("Size");
            }
        }

        protected Int64 sizeInBytes = 0;

        public Int64 SizeInBytes
        {
            get { return sizeInBytes; }
            set
            {
                sizeInBytes = value;
                RaisePropertyChanged("SizeInBytes");
            }
        }

        protected ObservableCollection<NavigatableDropBoxFolderItem> contents = new ObservableCollection<NavigatableDropBoxFolderItem>();

        public ObservableCollection<NavigatableDropBoxFolderItem> Contents
        {
            get { return contents; }
            set
            {
                contents = value;
                RaisePropertyChanged("Contents");
            }
        }

        #endregion Bindables
    }
}